package com.quantdo.demo.edit;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorPart;

import com.quantdo.demo.date.TomcatManageFacadeElement;
import com.quantdo.service.TomcatManageService;

/**
 * Tomcat管理操作
 * @author USER
 *
 */
public class TomcatManageEditor extends EditorPart {
	private TableViewer tableViewer;
	@Override
	public void doSave(IProgressMonitor arg0) {
	}

	@Override
	public void doSaveAs() {
	}

	@Override
	public void init(IEditorSite arg0, IEditorInput arg1) throws PartInitException {
		this.setSite(arg0);
		this.setInput(arg1);
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		ViewForm form = new ViewForm(parent, SWT.NONE);
		form.setLayout(new FillLayout());
		
		
		ToolBar toolBar = new ToolBar(form, SWT.FLAT);
		ToolBarManager toolBarManager = new ToolBarManager(toolBar);
		toolBarManager.add(new UploadAction());
		toolBarManager.add(new RefreshAction(tableViewer));
		toolBarManager.add(new StartAction(tableViewer));
		toolBarManager.add(new RestartAction(tableViewer));
		toolBarManager.add(new StopAction(tableViewer));
		toolBarManager.update(true);	
		
		tableViewer = new TableViewer(form, SWT.MULTI|SWT.BORDER|SWT.FULL_SELECTION);
		
		Table table = tableViewer.getTable();
		table.setLayout(new FillLayout());
		table.setHeaderVisible(true);
		
		TableColumn columnId = new TableColumn(table, SWT.LEFT);
		columnId.setText(" ID ");
		columnId.setResizable(true);
		columnId.pack();
		
		TableColumn columnPid = new TableColumn(table, SWT.LEFT);
		columnPid.setText("进程ID");
//		columnPid.pack();
		columnPid.setWidth(50);

		TableColumn columnUser = new TableColumn(table, SWT.LEFT);
		columnUser.setResizable(true);
		columnUser.setText("运行用户");
//		columnUser.pack();
		columnUser.setWidth(70);

		TableColumn columnPath = new TableColumn(table, SWT.LEFT);
		columnPath.setText("路径");
		columnPath.setWidth(350);
		
		
		TableColumn columnFileUser = new TableColumn(table, SWT.LEFT);
		columnFileUser.setResizable(true);
		columnFileUser.setText("文件所属");
//		columnFileUser.pack();
		columnFileUser.setWidth(70);
		TableColumn columnFileGroup = new TableColumn(table, SWT.LEFT);
		columnFileGroup.setResizable(true);
		columnFileGroup.setText("文件所属组");
//		columnFileGroup.pack();
		columnFileGroup.setWidth(80);
		
		TableColumn columnStatus = new TableColumn(table, SWT.LEFT);
		columnStatus.setText("状态");
		columnStatus.pack();
//		columnStatus.setWidth(60);

		TableColumn columnOpera = new TableColumn(table, SWT.LEFT);
		columnOpera.setText("操作");
//		columnOpera.setWidth(60);
		columnOpera.pack();
		
		tableViewer.setContentProvider(new TomcatManageContentProvider());
		tableViewer.setLabelProvider(new TomcatManageLabelProvider());
		tableViewer.setInput(TomcatManageService.getInstance().getTomcatInfo());
		form.setTopLeft(toolBar);
		form.setContent(tableViewer.getControl());
		
		//---
		//TableItem tableItem = table.getItem(0);
		//TableEditor stableEditor = new TableEditor(table);
		//Button startButton = new Button(table, SWT.PUSH);
		//startButton.setText("启动");
		//startButton.setBounds(10, 10, 10, 10);
		//stableEditor.setEditor(startButton, tableItem, 5);
//		stableEditor.setEditor(startButton, tableItem, 6);
		//---

		//TableItem item = new TableItem(table, SWT.BORDER);
		//TableEditor editor = new TableEditor(table);
		//editor.grabHorizontal=true;

		
//		Button startButton = new Button(table, SWT.PUSH);
//		startButton.setText("启动");
//		startButton.setBounds(10, 10, 10, 10);
		//Button restartButton = new Button(table, SWT.PUSH);
		//restartButton.setText("重启");
		//restartButton.setBounds(10, 10, 10, 10);
//		Button stopButton = new Button(table, SWT.PUSH);
//		stopButton.setText("关闭");

//		tableEditor.setEditor(startButton, item, 0);
		//editor.setEditor(restartButton, item, 2);
//		tableEditor.setEditor(stopButton, item, 2);
		//stableEditor.setItem(tableItem);

	}

	@Override
	public void setFocus() {
	}

	class UploadAction extends Action{

		public UploadAction() {
			setToolTipText("上传信息");
			setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_UP));
		}

		@Override
		public void run() {
			super.run();
		}
		
	}
	class RefreshAction extends Action{
		public RefreshAction(TableViewer tableViewer) {
			setToolTipText("刷新");
			setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ELCL_SYNCED));
		}

		@Override
		public void run() {
			RefreshAction action = this;
			this.setEnabled(false);
			new Thread(()->{
				try {
					Thread.sleep(5000);
				} catch (Exception e) {
					e.printStackTrace();
				} finally{
					action.setEnabled(true);
				}
			}).start();
			tableViewer.setInput(TomcatManageService.getInstance().getTomcatInfo());
		}
	}
	class StartAction extends Action{
		public StartAction(TableViewer tableViewer) {
			setToolTipText("启动");
			setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_DND_TOP_SOURCE));
		}

		@Override
		public void run() {
			IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
			TomcatManageFacadeElement obj = (TomcatManageFacadeElement)selection.getFirstElement();
			if(null == obj || null == obj.getPath())return;
			TomcatManageService.getInstance().startTomcat(obj.getPath());
			tableViewer.setInput(TomcatManageService.getInstance().getTomcatInfo());
		}
	}
	class RestartAction extends Action{
		public RestartAction(TableViewer tableViewer) {
			setToolTipText("重启");
			setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ETOOL_HOME_NAV));
		}

		@Override
		public void run() {
			IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
			TomcatManageFacadeElement obj = (TomcatManageFacadeElement)selection.getFirstElement();
			if(null == obj || null == obj.getPath())return;
			if(null != obj.getPid()){
				TomcatManageService.getInstance().stopTomcat(obj.getPid(), obj.getPath());
			}
			TomcatManageService.getInstance().startTomcat(obj.getPath());
			tableViewer.setInput(TomcatManageService.getInstance().getTomcatInfo());
		}
	}
	class StopAction extends Action{
		public StopAction(TableViewer tableViewer) {
			setToolTipText("停止");
			setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ELCL_STOP));
		}

		@Override
		public void run() {
			IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
			TomcatManageFacadeElement obj = (TomcatManageFacadeElement)selection.getFirstElement();
			if(null == obj || null == obj.getPid() || null == obj.getPath())return;
			TomcatManageService.getInstance().stopTomcat(obj.getPid(), obj.getPath());
			tableViewer.setInput(TomcatManageService.getInstance().getTomcatInfo());
		}
	}
}
